layui.use('form', function () {
    var form = layui.form;
    var id = getParam("id");
    var detail = getParam("detail");
    dict2Select("fieldType","externalType",/date/);
    dict2Select("fieldType","innerType");
    form.render();
    var isAdd=true;
    if (!isNull(id)) {
		$("#innerCName").attr("disabled","disabled");
    	isAdd=false;
    	io.get('/mm/fields/' + id,function(res){
    		form.val('editForm',res.data);
    		if(!isNull(detail)){
    			disableAllElements();
    			$("#saveBtn").hide();
    		}
    	});
    }
    function closeAndRefresh(){
    	closePage();
    	window.parent.document.getElementById("searchButton").click();
    }
    //监听提交
    form.on('submit(save)', function (formData) {
    	var url = "/mm/fields";
    	var data = JSON.stringify(formData.field);
    	var innerCName=JSON.parse(data).innerCName;
    	var checkData = {"innerName":innerCName};//这里借用列表查询接口，所以要传分页参数
    	if(isAdd){
    		io.post("/mm/fields/available-icname",JSON.stringify(checkData),function(res){
    			if(res.data==false){
					layer.alert("已经存在的内部字段中文名["+innerCName+"]!", {icon: 2});
    			}else{
					io.post(url,data,function(res){
        	        	closeAndRefresh();
        	        });
				}
    		},false,false);
	    }else{
	    	io.put(url,data,function(res){
	        	closeAndRefresh();
	        });
	    }
        return false;
    });
});

